package com.lianziyou.chain.common.email;

import lombok.extern.slf4j.Slf4j;
import org.cloud.utils.CommonUtil;
import org.cloud.utils.SpringContextUtil;
import org.cloud.utils.process.ProcessUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * 发送邮件工具类
 */
@Slf4j
public final class EmailUtil {

    /**
     * Spring Boot 提供了一个发送邮件的简单抽象，使用的是下面这个接口
     */
    private static JavaMailSender mailSender;


    /**
     * 配置文件中邮箱
     */
    private static String from;

    private static EmailUtil instance = new EmailUtil();
    ;

    private EmailUtil() {
        from = CommonUtil.single().getEnv("spring.mail.from", "");
        mailSender = SpringContextUtil.getBean(JavaMailSender.class);
    }


    public static EmailUtil single() {
        return instance;
    }

    /**
     * 异步发送邮件
     *
     * @param message
     */
    private void send(Object message) {
        ProcessUtil.single().submitRunnable(() -> {
            if (message instanceof SimpleMailMessage) {
                mailSender.send((SimpleMailMessage) message);
            } else if (message instanceof MimeMessage) {
                mailSender.send((MimeMessage) message);
            }
        });
    }

    /**
     * 简单文本邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        send(message);
        log.info("简单文本邮件提交成功, to={}, subject={}, content={}", to, subject, content);
    }

    /**
     * 简单文本邮件/别名
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMailAlias(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        String nick="";
        try {
            nick=javax.mail.internet.MimeUtility.encodeText("KIDU");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message.setFrom(nick+" <"+from+">");
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        send(message);
        log.info("简单文本邮件提交成功, to={}, subject={}, content={}", to, subject, content);
    }
    /**
     * html邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendHtmlMail(String to, String subject, String content) {
        //获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(subject);
            //邮件主题
            message.setSubject(subject);
            //邮件内容，html格式
            messageHelper.setText(content, true);
            //发送
            send(message);
            //日志信息
            log.info("HTML邮件提交成功, to={}, subject={}, content={}", to, subject, content);
        } catch (javax.mail.MessagingException e) {
            log.error("发送邮件时发生异常！{}", e);
        }
    }

    /**
     * 带附件的邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param filePath 附件
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            send(message);
            //日志信息
            log.info("附件邮件提交成功,to={}, subject={}, content={}, filePath={}", to, subject, content, filePath);
        } catch (javax.mail.MessagingException e) {
            log.error("发送邮件时发生异常！{}", e);
        }


    }
}
